<?php 
class conndb{
	private $conn;
	public function __construct($host,$user,$pwd,$dbname,$charset){
		//连接数据库
    $this->conn = @mysql_connect($host,$user,$pwd) or die('数据库连接失败,'.mysql_error());
    //选择数据库
    @mysql_select_db($dbname) or die('选择数据库失败；'.mysql_error());
    //设置字符集
    @mysql_set_charset($charset);
	}
    //query用于执行SQL语句，如果是SELECT语句，则返回查询结果集数组，否则返回true or false
	function query($sql){
		//截取SQL语句的前6个字符
		$sqltype = strtolower(substr(trim($sql),0,6));
		//执行SQL语句
		$result = mysql_query($sql,$this->conn);
		//定义二维数组用存储select语句的查询结果
		$callback_array = array();
		//判断SQL语句的类型
		//如果是SELECT语句
		if ("select"== $sqltype) {
			//如果SELECT语句有语法错误
			if ($result == false) {
				return false;
				//如果查询结果为空
			} else if(0 == mysql_num_rows($result)){
				return false;
			} else if(1 == mysql_num_rows($result)){
				return mysql_fetch_array($result);
			} else {
				while ($result_array = mysql_fetch_array($result)) {
					array_push($callback_array, $result_array);
				}
				return $callback_array;
			}
			
		}else if('update' == $sqltype || 'insert' == $sqltype || 'delete' == $sqltype){
			return $result;
		}
	}
	function __destruct(){
		mysql_close($this ->conndb);
	}
}


 ?>